// formPackage/pages/team/companyTeam/companyTeam.js
import {
  companyCheckEmployeeList,
  companyEmployeeList,
  deleteCompanyEmployee,
  myNextTeamList,
  companyCheckAction,
} from "../../../../service/team_service"
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    topTypeIndex: 0,//0:达人  0:公司成员，1:待审核
    companyList: [
      // { type: 1 }, { type: 1 }, { type: 1 }, { type: 1 },
      // { type: 1 }, { type: 1 }, { type: 1 }, { type: 1 },
    ],
    checkList: [],
    userList: [],
    page: 0,
    pageSize: 200,
    isManager: false,//是否是老板
    roleType: 1,//
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.companyEmployeeList();
    if (app.globalData.roleType) {
      this.setData({
        roleType: app.globalData.roleType
      })
      if (this.data.roleType == 9) {
        this.setData({
          isManager: true
        })
      }
    }
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
  },
  //顶部切换
  topChangeAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topTypeIndex: index
    })
    if (index == 0) {
      this.companyEmployeeList();
    } else if (index == 1) {
      this.companyCheckEmployeeList();
    }
  },


  //获取达人列表
  myNextTeamList() {
    wx.showLoading();
    myNextTeamList(this.data.page, this.data.pageSize).then(res => {
      wx.hideLoading();
      console.log("JJLog:达人列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          userList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:myNextTeamList", err);
    })
  },
  //公司列表:
  companyEmployeeList() {
    wx.showLoading();
    companyEmployeeList(this.data.page, this.data.pageSize).then(res => {
      wx.hideLoading();
      console.log("JJLog:公司列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          companyList: res.data || []
        })
        // if (res.data && res.data.length > 0) {
        //   this.dealManager(res.data);
        // }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:companyEmployeeList", err);
    })
  },
  dealManager(arr) {
    let isManager = false;
    arr.forEach((item) => {

      if (item.name == app.globalData.name && item.role == "老板") {
        isManager = true;
      }
    })
    this.setData({
      isManager: isManager,
    })
  },
  companyCheckEmployeeList() {
    wx.showLoading();
    companyCheckEmployeeList(this.data.page, this.data.pageSize).then(res => {
      wx.hideLoading();
      console.log("JJLog:审核列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          checkList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:companyCheckEmployeeList", err);
    })
  },
  //审核：
  checkEmAction(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let isPass = false;
    let showText = "是否拒绝成员入驻";
    if (index == 1) {
      isPass = true;
      showText = "是否同意成员入驻"
    }
    wx.showModal({
      title: '提示',
      content: showText,
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.companyCheckAction(id, isPass);
        }
      }
    })



  },
  companyCheckAction(id, isPass) {
    wx.showLoading();
    companyCheckAction(id, isPass).then(res => {
      wx.hideLoading();
      console.log("JJLog:审核列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "success"
        })
        this.companyCheckEmployeeList();
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:companyCheckAction", err);
    })
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    let path = "";
    let title = "";
    let imageUrl = "";
    let name = app.globalData.name || app.globalData.nickName
    if (this.data.topTypeIndex == 10) {
      //达人分享
      path = "/pages/home/home?isShare=1" + "&userId=" + app.globalData.userId
      title = name + "邀请你加入他的团队";
      imageUrl = "https://saas.img.ylyc.top/share/haoyouyqdrrz_icon.png"
    } else {
      //公司邀请:
      if (!app.globalData.companyNumber) {
        wx.showToast({
          title: '您还不是公司成员，不能邀请成员',
          icon: "none"
        })
        return;
      }
      path = "/formPackage/pages/team/inviteCompanyMember/inviteCompanyMember?companyNumber=" + app.globalData.companyNumber + "&userId=" + app.globalData.userId
      title = name + "邀请您加入" + app.globalData.companyName
      imageUrl = "https://saas.img.ylyc.top/share/haoyyqgsrz_icon.png"
    }

    return {
      title: title,
      path: path,
      imageUrl: imageUrl
    }
  },

  //删除一个员工:
  rightCancelBtnAction(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '是否删除该公司员工',
      success: res => {
        if (res.confirm) {
          this.deleteCompanyEmployee(id, index);
        }
      }
    })

  },
  deleteCompanyEmployee(id, index) {
    wx.showLoading();
    deleteCompanyEmployee(id).then(res => {
      wx.hideLoading();
      console.log("JJLog:审核列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '删除成功',
          icon: "success"
        })
        let arr = this.data.companyList;
        arr.splice(index, 1);
        this.setData({
          companyList: arr,
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:deleteCompanyEmployee", err);
    })
  },
  //打电话
  callPhoneAction(e) {
    let phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone,
      fail: err => {

      }
    })
  },

})